// pages/doctor/doctor.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: [],
    pic: 0,
    num: "2万",
    picArr: [],
    countPic: '',
    idArr: [],
    xitong: "",
    kfIndex: -1,
    types: 0,
    data_arr: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var bgc = wx.getStorageSync('color')
    var types = options.types
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    wx.showLoading({
      title: '搜索中请稍后',
    })
    //接上个页面的数据
    var kewords = JSON.parse(options.kewords)
    var imgArr = JSON.parse(options.imgArr)
    app.util.request({
      'url': 'entry/wxapp/Search.searching',
      data: {
        kewords: kewords
      },
      success: function(res) {
        console.log(res)
        wx.hideLoading()
        that.setData({
          arr: res.data.data
        })
      },
      fail: function(err) {
        console.log(err)
      },
    })
    that.setData({
      bgc: bgc,
      kewords: kewords,
      conArr: imgArr
    })
  },
  checkboxChange: function(e) {
    var that = this
    var picArr = that.data.picArr
    var idArr = that.data.idArr
    var countPic = Number(that.data.countPic)
    var wzmoney = that.data.wzmoney
    var arr = that.data.arr
    var xitong = that.data.xitong
    var zid = e.currentTarget.dataset.id
    if (that.findThis(picArr, e.currentTarget.dataset.index)) {
      for (var i = 0, len = picArr.length; i < len; i++) {
        if (picArr[i] == e.currentTarget.dataset.index) {
          // console.log(picArr,i)
          picArr.splice(i, 1)
          if (e.currentTarget.dataset.index === -1) {
            countPic -= Number(wzmoney)
            idArr.splice(i, 1)
          } else {
            // console.log(idArr,i)
            countPic -= Number(arr[e.currentTarget.dataset.index].wzmoney)
            idArr.splice(i, 1)
          }
        }
      }
    } else {
      picArr.push(e.currentTarget.dataset.index)
      if (e.currentTarget.dataset.index === -1) {
        countPic += Number(wzmoney)
        console.log(e.currentTarget.dataset.id)
        idArr.push(e.currentTarget.dataset.id)
      } else {
        // console.log(arr)
        countPic += Number(arr[e.currentTarget.dataset.index].wzmoney)
        idArr.push(arr[e.currentTarget.dataset.index].zid)
      }
    }
    console.log(idArr)
    console.log(picArr)
    that.setData({
      picArr: picArr,
      countPic: countPic.toFixed(2),
      idArr: idArr,
    })
  },
  //查找数组中 有没有这个数字
  findThis: function(arr, index) {
    for (var i = 0, len = arr.length; i < len; i++) {
      if (arr[i] == index) {
        return true
      }
    }
    return false
  },
  //提交
  complete: function(e) {
    var that = this
    var countPic = that.data.countPic
    var idArr = that.data.idArr
    var conArr = that.data.conArr
    var picArr = that.data.picArr
    var f_id = wx.getStorageSync('openid')
    var kewords = that.data.kewords
    if (picArr.length == '') {
      wx.showToast({
        title: '请选择医生',
        icon: 'none'
      })
    } else {
      //
      if (countPic > 0.00) {
        app.util.request({
          'url': that.data.url + 'app/index.php?i=' + that.data.uniacid + '&c=entry&a=wxapp&do=Pay&m=hyb_yl',
          header: {
            "Content-Type": "application/xml"
          },
          method: "GET",
          data: {
            openid: wx.getStorageSync('openid'),
            z_tw_money: countPic
          },
          success: function(res) {
            //支付成功后对话
            wx.requestPayment({
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonceStr, //字符串随机数
              'package': res.data.package,
              'signType': res.data.signType,
              'paySign': res.data.paySign,
              success: function(res) {
                var data_arr = that.data.data_arr;
                for (var i = 0; i < conArr.length; i++) {
                  wx.uploadFile({
                    url: that.data.url + 'app/index.php?i=' + that.data.uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
                    filePath: conArr[i],
                    name: 'upfile',
                    formData: [],
                    success: function(res) {
                      console.log(res)
                      data_arr.push(res.data)
                      that.setData({
                        data_arr: data_arr
                      })
                    }
                  })
                }
                wx.showLoading({
                  title: '提交中，请稍后',
                  mask: !0
                })
                //提交
                setTimeout(function() {
                  var f_ipic = that.data.data_arr
                  app.util.request({
                    'url': 'entry/wxapp/Tuwen.addkuaisu',
                    data: {
                      msg: kewords,
                      data_arr: f_ipic,
                      useropenid: wx.getStorageSync('openid'), //患者openid
                      keywords: 'kuaisuwenzhen',
                      type: 0,
                      zid: idArr,
                      money: countPic,
                      name: '快速问诊'
                    },
                    success: function(res) {
                      console.log(res)
                      
                    }
                  })
                  wx.hideLoading()
                }, 2000)
              }
            })

          }
        })
      } else {
        var data_arr = that.data.data_arr;
        for (var i = 0; i < conArr.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + that.data.uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: conArr[i],
            name: 'upfile',
            formData: [],
            success: function(res) {
              console.log(res)
              data_arr.push(res.data)
              that.setData({
                data_arr: data_arr
              })
            }
          })
        }
        wx.showLoading({
          title: '提交中，请稍后',
          mask: !0
        })
        //提交
        setTimeout(function() {
          var f_ipic = that.data.data_arr
          app.util.request({
            'url': 'entry/wxapp/Tuwen.addkuaisu',
            data: {
              msg: kewords,
              data_arr: f_ipic,
              useropenid: wx.getStorageSync('openid'), //患者openid
              keywords: 'kuaisuwenzhen',
              type: 0,
              zid: idArr,
              money: countPic,
              name: '快速问诊'
            },
            success: function(res) {
              console.log(res)
            }
          })
          wx.hideLoading()
        }, 2000)

      }

    }



  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  //查询一条客服id
  // getKfuid: function () {
  //   var that = this
  //   app.util.request({
  //     'url': 'entry/wxapp/Kfuid',
  //     success: function (res) {
  //       console.log(res)
  //       that.setData({
  //         kfid: res.data.zid,
  //         wzmoney: res.data.wzmoney,
  //       })
  //     }
  //   })
  // }
})